const indexs = [0, 1, 0, 1];
const treeH = [
  {
    id: '1',
    label: '第一个',
    children: [
      {
        id: '1-1',
        label: '第一个-1',
        children: [],
      },
      {
        id: '1-2',
        label: '第一个-2',
        children: [
          {
            id: '1-2-1',
            label: '第一个-2-1',
            children: [
              {
                id: '1-2-1-1',
                label: '第一个-2-1-1',
                children: [],
              },
              {
                id: '1-2-1-2',
                label: '第一个-2-1-2',
                children: [],
              },
            ],
          },
        ],
      },
    ],
  },
  {
    id: '2',
    label: '第二个',
    children: [
      {
        id: '2-1',
        label: '第二个-1',
        children: [],
      },
    ],
  },
];

console.log(indexs, '---');

function getTreeItem(treeObj, indexs) {
  let item = treeObj || {};
  indexs.forEach(function (index) {
    if (Array.isArray(item.children)) {
      item = item.children[index];
    } else {
      item = item[index];
    }
  });
  return item;
}

function setTreeItem(treeObj, indexs, updateData) {
  const item = getTreeItem(treeObj, indexs);
  Object.assign(item, updateData);
  return item;
}

// 简单hook
function useTreeItem(treeObj) {
  return [
    (indexs) => getTreeItem(treeObj, indexs),
    (indexs, updateData) => setTreeItem(treeObj, indexs, updateData),
  ];
}

// 使用hook
const [getItem, setItem] = useTreeItem(treeH);

console.log(getItem(indexs), 'getTreeItem 修改前');

console.log(setItem(indexs, { msg: '我被修改了' }), 'setTreeItem');

console.log(getItem(indexs), 'getTreeItem');

console.log(JSON.stringify(treeH), 'new treeH');
